<?php

namespace app\controllers\merchant\shop;

use app\models\merchant\system\OperationRecordModel;
use app\models\merchant\user\MerchantModel;
use app\models\shop\ShopGoodsModel;
use app\models\shop\ShopSpecialModel;
use yii;
use yii\db\Exception;
use yii\web\MerchantController;

/**
 * 应用类目表控制器
 * 地址:/admin/rule
 * @throws Exception if the model cannot be found
 * @return array
 */
class SpecialController extends MerchantController {

    public $enableCsrfValidation = false; //禁用CSRF令牌验证，可以在基类中设置

    /**
     * 地址:/admin/group/index 默认访问
     * @throws Exception if the model cannot be found
     * @return array
     */

    public function actionList() {
        if (yii::$app->request->isGet) {
            $request = yii::$app->request; //获取 request 对象
            $params = $request->get(); //获取地址栏参数

            $must = ['key'];
            $rs = $this->checkInput($must, $params);
            if ($rs != false) {
                return $rs;
            }

            $model = new ShopSpecialModel();
            $params['merchant_id'] = yii::$app->session['uid'];
            $array = $model->do_select($params);
            if ($array['status'] == 200) {
                $time = time();
                foreach ($array['data'] as $k=>$v){
                    if (!empty($v['goods_ids'])) {
                        $goodsIds = json_decode($v['goods_ids'],true);
                        $array['data'][$k]['goods_num'] = count($goodsIds);
                    } else {
                        $array['data'][$k]['goods_num'] = 0;
                    }
                    if ($v['start_time'] > $time) {
                        $array['data'][$k]['activity_status'] = '未开始';
                    } elseif ($v['end_time'] < $time) {
                        $array['data'][$k]['activity_status'] = '已结束';
                    } else {
                        $array['data'][$k]['activity_status'] = '进行中';
                    }
                    $array['data'][$k]['start_time'] = date('Y-m-d H:i:s',$v['start_time']);
                    $array['data'][$k]['end_time'] = date('Y-m-d H:i:s',$v['end_time']);
                }
            }
            return $array;
        } else {
            return result(500, "请求方式错误");
        }
    }

    public function actionOne($id) {
        if (yii::$app->request->isGet) {
            $request = yii::$app->request; //获取 request 对象
            $params = $request->get(); //获取地址栏参数

            $model = new ShopSpecialModel();
            $where['id'] = $id;
            $array = $model->do_one($where);
            if ($array['status'] == 200) {
                $array['data']['start_time'] = date('Y-m-d H:i:s',$array['data']['start_time']);
                $array['data']['end_time'] = date('Y-m-d H:i:s',$array['data']['end_time']);
                $array['data']['pic_urls'] = explode(',',$array['data']['pic_urls']);

                if (!empty($array['data']['goods_ids'])) {
                    $goodsModel = new ShopGoodsModel();
                    $goodsIds = json_decode($array['data']['goods_ids'],true);
                    $goodsWhere['field'] = "id,name,pic_urls,price";
                    $goodsWhere['in'] = ['id',$goodsIds];
                    $goodsWhere['limit'] = false;
                    $goodsInfo = $goodsModel->do_select($goodsWhere);
                    if ($goodsInfo['status'] != 200){
                        return result(500, "商品信息有误");
                    }
                    foreach ($goodsInfo['data'] as $k=>$v){
                        $pic = explode(',',$v['pic_urls']);
                        $goodsInfo['data'][$k]['pic_urls'] = $pic[0];
                    }
                    $array['data']['goods'] = $goodsInfo['data'];
                }
            }
            return $array;
        } else {
            return result(500, "请求方式错误");
        }
    }

    public function actionAdd() {
        if (yii::$app->request->isPost) {
            $request = yii::$app->request; //获取 request 对象
            $params = $request->bodyParams; //获取body传参

            $must = ['key','name','pic_urls','bg_color','start_time','end_time','is_show_sales','goods_style','share_name','share_url','sort','goods_ids'];
            $rs = $this->checkInput($must, $params);
            if ($rs != false) {
                return $rs;
            }

            $model = new ShopSpecialModel();
            $data['key'] = $params['key'];
            $data['merchant_id'] = yii::$app->session['uid'];
            $data['name'] = $params['name'];
            if (isset($params['pic_urls'])){
                $data['pic_urls'] = implode(',',$params['pic_urls']);
            }
            $data['bg_color'] = $params['bg_color'];
            $data['start_time'] = strtotime($params['start_time']);
            $data['end_time'] = strtotime($params['end_time']);
            $data['is_show_sales'] = $params['is_show_sales'];
            $data['goods_style'] = $params['goods_style'];
            $data['share_name'] = $params['share_name'];
            $data['share_url'] = $params['share_url'];
            $data['sort'] = $params['sort'];
            $data['goods_ids'] = json_encode($params['goods_ids']);
            $array = $model->do_add($data);
            if ($array['status'] == 200){
                //添加操作记录
                $operationRecordModel = new OperationRecordModel();
                $operationRecordData['key'] = $params['key'];
                if (isset(yii::$app->session['sid'])) {
                    $subModel = new \app\models\merchant\system\UserModel();
                    $subInfo = $subModel->find(['id'=>yii::$app->session['sid']]);
                    if ($subInfo['status'] == 200){
                        $operationRecordData['merchant_id'] = $subInfo['data']['username'];
                    }
                } else {
                    $merchantModle = new MerchantModel();
                    $merchantInfo = $merchantModle->find(['id'=>yii::$app->session['uid']]);
                    if ($merchantInfo['status'] == 200) {
                        $operationRecordData['merchant_id'] = $merchantInfo['data']['name'];
                    }
                }
                $operationRecordData['operation_type'] = '新增';
                $operationRecordData['operation_id'] = $array['data'];
                $operationRecordData['module_name'] = '专题';
                $operationRecordModel->do_add($operationRecordData);
            }
            return $array;
        } else {
            return result(500, "请求方式错误");
        }
    }

    public function actionUpdate($id) {
        if (yii::$app->request->isPut) {
            $request = yii::$app->request; //获取 request 对象
            $params = $request->bodyParams; //获取body传参

            $must = ['key'];
            $rs = $this->checkInput($must, $params);
            if ($rs != false) {
                return $rs;
            }

            $model = new ShopSpecialModel();
            $where['id'] = $id;
            if (isset($params['pic_urls'])){
                $params['pic_urls'] = implode(',',$params['pic_urls']);
            }
            if (isset($params['goods_ids'])) {
                $params['goods_ids'] = json_encode($params['goods_ids']);
            }
            if (isset($params['start_time'])){
                $params['start_time'] = strtotime($params['start_time']);
            }
            if (isset($params['end_time'])){
                $params['end_time'] = strtotime($params['end_time']);
            }
            $array = $model->do_update($where,$params);
            if ($array['status'] == 200){
                //添加操作记录
                $operationRecordModel = new OperationRecordModel();
                $operationRecordData['key'] = $params['key'];
                if (isset(yii::$app->session['sid'])) {
                    $subModel = new \app\models\merchant\system\UserModel();
                    $subInfo = $subModel->find(['id'=>yii::$app->session['sid']]);
                    if ($subInfo['status'] == 200){
                        $operationRecordData['merchant_id'] = $subInfo['data']['username'];
                    }
                } else {
                    $merchantModle = new MerchantModel();
                    $merchantInfo = $merchantModle->find(['id'=>yii::$app->session['uid']]);
                    if ($merchantInfo['status'] == 200) {
                        $operationRecordData['merchant_id'] = $merchantInfo['data']['name'];
                    }
                }
                $operationRecordData['operation_type'] = '更新';
                $operationRecordData['operation_id'] = $id;
                $operationRecordData['module_name'] = '专题';
                $operationRecordModel->do_add($operationRecordData);
            }
            return $array;
        } else {
            return result(500, "请求方式错误");
        }
    }

    public function actionDelete($id) {
        if (yii::$app->request->isDelete) {
            $request = yii::$app->request; //获取 request 对象
            $params = $request->bodyParams; //获取body传参

            $must = ['key'];
            $rs = $this->checkInput($must, $params);
            if ($rs != false) {
                return $rs;
            }

            $model = new ShopSpecialModel();
            $where['id'] = $id;
            $array = $model->do_delete($where);
            if ($array['status'] == 200){
                //添加操作记录
                $operationRecordModel = new OperationRecordModel();
                $operationRecordData['key'] = $params['key'];
                if (isset(yii::$app->session['sid'])) {
                    $subModel = new \app\models\merchant\system\UserModel();
                    $subInfo = $subModel->find(['id'=>yii::$app->session['sid']]);
                    if ($subInfo['status'] == 200){
                        $operationRecordData['merchant_id'] = $subInfo['data']['username'];
                    }
                } else {
                    $merchantModle = new MerchantModel();
                    $merchantInfo = $merchantModle->find(['id'=>yii::$app->session['uid']]);
                    if ($merchantInfo['status'] == 200) {
                        $operationRecordData['merchant_id'] = $merchantInfo['data']['name'];
                    }
                }
                $operationRecordData['operation_type'] = '删除';
                $operationRecordData['operation_id'] = $id;
                $operationRecordData['module_name'] = '专题';
                $operationRecordModel->do_add($operationRecordData);
            }
            return $array;
        } else {
            return result(500, "请求方式错误");
        }
    }

    //查询商品
    public function actionGoods() {
        if (yii::$app->request->isGet) {
            $request = yii::$app->request; //获取 request 对象
            $params = $request->get(); //获取地址栏参数

            $must = ['key','supplier_id'];
            $rs = $this->checkInput($must, $params);
            if ($rs != false) {
                return $rs;
            }

            $model = new ShopGoodsModel();
            if (isset($params['searchName'])) {
                if ($params['searchName'] != "") {
                    $params['name'] = ['like', "{$params['searchName']}"];
                }
                unset($params['searchName']);
            }
            $params['field'] = "id,supplier_id,name,pic_urls,price";
            if (isset($params['supplier_id']) && $params['supplier_id'] != 0){
                $params['is_check'] = 1;
            }
            $params['status'] = 1;
            $params['or'] = ['and',['!=','is_open_assemble', 1],['!=','is_bargain', 1]];
            $params['merchant_id'] = yii::$app->session['uid'];
            $array = $model->do_select($params);
            if ($array['status'] == 200){
                foreach ($array['data'] as $k=>$v){
                    $pic = explode(',',$v['pic_urls']);
                    $array['data'][$k]['pic_urls'] = $pic[0];
                }
            }
            return $array;
        } else {
            return result(500, "请求方式错误");
        }
    }





}
